<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\helper;

use app\admin\common\Pagination;

final class Table
{
    public function render_header_select($options = array())
    {
        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'check-all';
        $attributes = array_merge($attributes, $options);
        return helper('admin/html')->render_tag('input', $attributes);
    }

    public function render_header_text($text, $options = array())
    {
        if (empty($options)) {
            return $text;
        }

        $attributes = $options;
        return helper('admin/html')->render_tag('span', $attributes, $text);
    }

    public function render_column_select($value, $options = array())
    {
        if (isset($options['selected'])) {
            $selected = $options['selected'];
            unset($options['selected']);
        } else {
            $selected = array();
        }

        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'ids';
        $attributes['name'] = 'ids[]';
        $attributes['value'] = $value;
        in_array($value, $selected) && $attributes['checked'] = 'checked';
        $attributes = array_merge($attributes, $options);
        return helper('admin/html')->render_tag('input', $attributes);
    }

    public function render_column_text($value, $options = array())
    {
        if (isset($options['list'])) {
            $list = (array) $options['list'];
            unset($options['list']);
        } else {
            $list = array();
        }

        if (isset($options['link'])) {
            $link = $options['link'];
            unset($options['link']);
        } else {
            $link = '';
        }

        if (isset($options['format'])) {
            $format = $options['format'];
            unset($options['format']);

            if ($value) {
                $value = helper('admin/format')->$format($value);
            }
        }

        if ($list) {
            if (array_key_exists($value, $list)) {
                $display = $list[$value];

                if (is_array($display)) {
                    $text = array_shift($display);
                    $class = array_shift($display);

                    $attributes = $options;
                    $attributes['class'] = $class;
                    $display = helper('admin/html')->render_tag('span', $attributes, $text);
                }
            } else {
                $display = '';
            }
        } else {
            $display = htmlspecialchars($value);
        }

        if ($link) {
            $attributes = $options;
            $attributes['href'] = $link;
            $display = helper('admin/html')->render_tag('a', $attributes, $display);
        }

        return $display;
    }

    public function render_column_url($value, $options = array())
    {
        if ($value) {
            $attributes['href'] = $value;
            $attributes['class'] = 'url';
            $attributes['target'] = '_blank';
            $attributes = array_merge($attributes, $options);
            return helper('admin/html')->render_tag('a', $attributes, $value);
        } else {
            return '';
        }
    }

    public function render_column_status($value, $options = array())
    {
        $options['list'] = array(
            0 => array('无效', 'label label-danger'),
            1 => array('有效', 'label label-success'),
        );
        return $this->render_column_text($value, $options);
    }

    public function render_column_image($value, $options = array())
    {
        if (intval($value) > 0) {
            $url = model('admin/upload')->get_url($value);
        } else {
            $url = $value;
        }

        if ($url) {
            $attributes['src'] = $url;
            $attributes['class'] = 'thumb';
            $attributes = array_merge($attributes, $options);
            return helper('admin/html')->render_tag('img', $attributes);
        } else {
            return '';
        }
    }

    public function render_column_date($value, $options = array())
    {
        if ($value) {
            $display = helper('admin/format')->date($value);
            $attributes['class'] = 'date_ts';
            $attributes = array_merge($attributes, $options);
            return helper('admin/html')->render_tag('span', $attributes, $display);
        } else {
            return '';
        }
    }

    public function render_column_date_time($value, $options = array())
    {
        if ($value) {
            $display = helper('admin/format')->date_time($value);
            $attributes['class'] = 'date_ts';
            $attributes = array_merge($attributes, $options);
            return helper('admin/html')->render_tag('span', $attributes, $display);
        } else {
            return '';
        }
    }

    public function render_action($text, $icon, $class, $options = array())
    {
        $icon_html = $icon ? helper('admin/html')->render_tag('i', array('class' => $icon)) : null;

        $attributes['type'] = 'button';
        $attributes['class'] = $class;
        $attributes['title'] = $text;
        $attributes = array_merge($attributes, $options);
        return helper('admin/html')->render_tag('button', $attributes, $icon_html);
    }

    public function render_empty_row($options = array())
    {
        $html = '';
        $html .= helper('admin/html')->render_start_tag('tr', array('class' => 'data-empty'));
        $html .= helper('admin/html')->render_start_tag('td', array('class' => 'text-center empty-info', 'colspan' => '50'));
        $html .= helper('admin/html')->render_tag('i', array('class' => 'fa fa-database'));
        $html .= helper('admin/html')->render_tag('span', null, '暂时没有数据');
        $html .= helper('admin/html')->render_end_tag('td');
        $html .= helper('admin/html')->render_end_tag('tr');
        return $html;
    }

    public function render_pagination($pagination, $page_url = null)
    {
        if (is_array($pagination)) {
            $page_object = new Pagination();
            $page_object->totalRows = $pagination['rows_count'];
            $page_object->listRows = $pagination['page_size'];
            $page_object->nowPage = $pagination['page_num'];
            $page_object->url = $page_url;
        } else {
            $page_object = $pagination;
            if ($page_url) {
                $page_object->page_url = $page_url;
            }
        }

        $html = '';
        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'row'));

        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'col-sm-5'));
        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'pagination-info'));
        $html .= $page_object->show_info();
        $html .= helper('admin/html')->render_end_tag('div');
        $html .= helper('admin/html')->render_end_tag('div');

        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'col-sm-7'));
        $html .= helper('admin/html')->render_start_tag('ul', array('class' => 'pagination'));
        $html .= $page_object->show_pages();
        $html .= helper('admin/html')->render_end_tag('ul');
        $html .= helper('admin/html')->render_end_tag('div');

        $html .= helper('admin/html')->render_end_tag('div');
        return $html;
    }

    public function render_paginationx($page_url = null, $total_rows, $page_num, $page_size, $sort_field, $sort_order)
    {
        $page_object = new Pagination();
        $page_object->totalRows = $total_rows;
        $page_object->listRows = $page_size;
        $page_object->nowPage = $page_num;
        $page_object->url = $page_url;

        $html = '';
        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'row'));

        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'col-sm-5'));
        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'pagination-info'));
        $html .= $page_object->show_info();
        $html .= helper('admin/html')->render_end_tag('div');
        $html .= helper('admin/html')->render_end_tag('div');

        $html .= helper('admin/html')->render_start_tag('div', array('class' => 'col-sm-7'));
        $html .= helper('admin/html')->render_start_tag('ul', array('class' => 'pagination'));
        $html .= $page_object->show_pages();
        $html .= helper('admin/html')->render_end_tag('ul');
        $html .= helper('admin/html')->render_end_tag('div');

        $html .= helper('admin/html')->render_end_tag('div');
        return $html;
    }
}